package com.vclee.fast.integration.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vclee.fast.integration.power.mapper.SysMenuMapper;
import com.vclee.fast.integration.power.mapper.SysRoleMenuMapper;
import com.vclee.fast.integration.power.model.vo.MenuTree;
import com.vclee.fast.integration.power.model.SysMenu;
import com.vclee.fast.integration.power.model.SysRoleMenu;
import com.vclee.fast.integration.power.service.SysMenuService;
import com.vclee.fast.utils.exception.Assert;
import com.vclee.fast.utils.model.Bson;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.vclee.fast.integration.power.service.impl.SysUserServiceImpl.getChildren;


@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl implements SysMenuService {

    private final SysMenuMapper menuMapper;
    private final SysRoleMenuMapper roleMenuMapper;


    @Override
    public void addMenu(SysMenu sysMenu) {
        if (sysMenu.getSortNum()==null) sysMenu.setSortNum(99);
        menuMapper.insert(sysMenu);
    }

    @Override
    public void editMenu(SysMenu sysMenu) {
        Assert.claim(sysMenu.getId() == null, "操作失败，主键为空");
        menuMapper.updateById(sysMenu);
    }

    @Override
    public void delMenu(SysMenu sysMenu) {
        Assert.claim(sysMenu.getId()==null,"操作失败，主键为空");
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getMenuId,sysMenu.getId());
        Assert.claim(roleMenuMapper.selectCount(wrapper)>0,"无法删除,存在角色绑定了该菜单");
        menuMapper.deleteById(sysMenu.getId());
    }

    @Override
    public List<MenuTree> findListByQuery(Bson query) {
        List<Integer> list = query.getArray("list",Integer.class);
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        if(list!=null && !list.isEmpty()) wrapper.notIn(SysMenu::getId,list.toArray());
        wrapper.orderByDesc(SysMenu::getSortNum);
        return getMenuTree(menuMapper.selectList(wrapper));
    }

    public static List<MenuTree> getMenuTree(List<SysMenu> menus) {
        List<MenuTree> menuList = new ArrayList<>();
        if(menus!=null && !menus.isEmpty()){
            for (SysMenu pgSysMenuVO : menus) {
                if (pgSysMenuVO.getParentId() == null) {
                    MenuTree menuTree = new MenuTree();
                    menuTree.setPath(pgSysMenuVO.getUrl());
                    menuTree.setId(pgSysMenuVO.getId());
                    menuTree.setTitle(pgSysMenuVO.getMenuName());
                    menuTree.setIcon(pgSysMenuVO.getIcon());
                    menuTree.setSortNum(pgSysMenuVO.getSortNum());
                    menuTree.setIsShow(pgSysMenuVO.getIsShow());
                    menuTree.setChildren(getChildren(menuTree, menus));
                    menuList.add(menuTree);
                }
            }
            if(!menuList.isEmpty()){
                menuList.sort(Comparator.comparing(MenuTree::getSortNum));
                Collections.reverse(menuList);
            }
        }
        return menuList;
    }
}
